﻿using System;
using DBProvider.Providers;

namespace HealthCommunity.Controls
{
	public partial class UCDrugStoreInfo : System.Web.UI.UserControl
	{
		private IDrugStoreProvider _DrugStoreProvider;

		protected void Page_Load(object sender, EventArgs e)
		{
			if (!IsPostBack)
			{
				var theUser = SiteContext.Current.CurrentUser;
				var isEditAble = false;

				if (theUser != null && theUser.Role != null)
				{
					isEditAble = theUser.Role.RoleName.Equals("admin") || theUser.Role.RoleName.Equals("storemanager") || theUser.Role.RoleName.Equals("storeadmin");
				}
				btnEdit.Visible = isEditAble;
				btnSave.Visible = false;

				txtDrugStoreInfoNew.Visible = false;
				txtDrugStoreInfoActivity.Visible = false;
				txtDrugStoreInfoDetail.Visible = false;
				pDrugStoreInfoActivity.Visible = true;
				pDrugStoreInfoDetail.Visible = true;
				pDrugStoreInfoNew.Visible = true;

				var theDrugStore = theUser.DrugStore;

				if (theDrugStore != null)
				{
					if (_DrugStoreProvider == null)
					{
						_DrugStoreProvider = new DrugStoreProvider();
					}

					var theDrugStoreInfo = _DrugStoreProvider.GetDrugStoreInfo(theDrugStore.DrugStoreID);

					if (theDrugStoreInfo != null)
					{
						txtDrugStoreInfoActivity.Text = theDrugStoreInfo.DrugStoreInfoActivity;
						txtDrugStoreInfoDetail.Text = theDrugStoreInfo.DrugStoreInfoDetail;
						txtDrugStoreInfoNew.Text = theDrugStoreInfo.DrugStoreInfoNews;
						pDrugStoreInfoNew.InnerText = theDrugStoreInfo.DrugStoreInfoNews;
						pDrugStoreInfoDetail.InnerText = theDrugStoreInfo.DrugStoreInfoDetail;
						pDrugStoreInfoActivity.InnerText = theDrugStoreInfo.DrugStoreInfoActivity;
					}
				}
			}
		}

		protected void btnSave_Click(object sender, EventArgs e)
		{
			if (SiteContext.Current.CurrentUser != null && SiteContext.Current.CurrentUser.DrugStore != null)
			{
				var theDrugStoreInfo = new DBProvider.DrugStoreInfo() { DrugStoreInfoActivity = txtDrugStoreInfoActivity.Text.Trim(), DrugStoreInfoNews = txtDrugStoreInfoNew.Text.Trim(), DrugStoreInfoDetail = txtDrugStoreInfoDetail.Text.Trim(), LastUpdate = DateTime.Now, DrugStoreID = SiteContext.Current.CurrentUser.DrugStoreID.GetValueOrDefault(0) };

				if (_DrugStoreProvider == null)
				{
					_DrugStoreProvider = new DrugStoreProvider();
				}

				try
				{
					_DrugStoreProvider.SaveDrugStoreInfo(theDrugStoreInfo);
					ltrMsg.Text = "保存成功";

					txtDrugStoreInfoNew.Visible = false;
					txtDrugStoreInfoActivity.Visible = false;
					txtDrugStoreInfoDetail.Visible = false;
					pDrugStoreInfoActivity.Visible = true;
					pDrugStoreInfoDetail.Visible = true;
					pDrugStoreInfoNew.Visible = true;

					pDrugStoreInfoNew.InnerText = txtDrugStoreInfoNew.Text;
					pDrugStoreInfoDetail.InnerText = txtDrugStoreInfoDetail.Text;
					pDrugStoreInfoActivity.InnerText = txtDrugStoreInfoActivity.Text;
				}
				catch (Exception exc)
				{
					ltrMsg.Text = exc.Message;
				}
			}
		}

		protected void btnEdit_Click(object sender, EventArgs e)
		{
			txtDrugStoreInfoActivity.Visible = true;
			txtDrugStoreInfoDetail.Visible = true;
			txtDrugStoreInfoNew.Visible = true;

			pDrugStoreInfoActivity.Visible = false;
			pDrugStoreInfoDetail.Visible = false;
			pDrugStoreInfoNew.Visible = false;

			btnEdit.Visible = false;
			btnSave.Visible = true;
		}
	}
}